const md5 = require('/utils/md5.js')
App({
    data: {
        apisite: "https://ldjx.ricke.cc/", //接口网址
        apiurl: "https://ldjx.ricke.cc/api/", //接口地址
        // apisite: "http://192.168.8.122:910/", //接口网址
        // apiurl: "http://192.168.8.122:910/api/", //接口地址
        appid: '', //appid
        appsecret: '', //密钥
        systemInfo: {},
        statusBarHeight: 0,
        navBarHeight: 0,
        refresh: 0
    },

    onLaunch() {
        var that = this
        that.wxlogin()
        that.autoUpdate()

        //获取设备信息
        const systemInfo = tt.getSystemInfoSync()
        const navBar = tt.getMenuButtonBoundingClientRect()
        that.data.systemInfo = systemInfo
        that.data.statusBarHeight = systemInfo.statusBarHeight
        that.data.navBarHeight = navBar.height + (navBar.top - systemInfo.statusBarHeight) * 2
    },

    // wxlogin
    wxlogin() {
        var that = this
        return new Promise(function (resolve,reject) {
            tt.login({
                success: (res) => {
                    const code = res.code
                    const anonymous_code = res.anonymousCode
                    const data = { code,anonymous_code }
                    // console.log(data)
                    that.api_request('?c=openid_tt','POST',data).then(function (res) {
                        tt.setStorageSync('userinfo',res)
                        resolve(res)
                    })
                },fail: (error) => {
                    console.log('登录失败' + error)
                }
            })
        })
    },

    //Api请求
    api_request: function (url,method,parm) {
        var apiurl = this.data.apiurl
        // var appid = this.data.appid
        // var appsecret = this.data.appsecret
        // var timestamp = Date.parse(new Date())
        // timestamp = timestamp / 1000
        // var signature = md5.hex_md5(md5.hex_md5(appid + appsecret + timestamp))
        var data = {
            // appid: appid,
            // timestamp: timestamp,
            // signature: signature,
        }
        if (method == 'POST') {
            var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }
        } else {
            method = 'GET'
            var header = { 'Content-Type': 'application/json' }
        }
        for (var i in parm) {
            data[i] = parm[i]
        }
        return new Promise(function (resolve,reject) {
            tt.request({
                method: method,
                url: apiurl + url,
                header: header,
                data: data,
                success: function (res) {
                    if (res.data) {
                        resolve(res.data)
                    }
                }
            })
        })
    },

    //上传类
    api_upload: function (url,filePath,parm) {
        var apiurl = this.data.apiurl
        // var appid = this.data.appid
        // var appsecret = this.data.appsecret
        // var timestamp = Date.parse(new Date())
        // timestamp = timestamp / 1000
        // var signature = md5.hex_md5(md5.hex_md5(appid + appsecret + timestamp))
        var data = {
            // appid: appid,
            // timestamp: timestamp,
            // signature: signature,
        }
        var header = { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' }
        for (var i in parm) {
            data[i] = parm[i]
        }
        return new Promise(function (resolve,reject) {
            tt.showLoading({
                title: '上传中'
            })
            tt.uploadFile({
                method: "POST",
                url: apiurl + 'upload.asp' + url,
                header: header,
                name: 'file',
                filePath: filePath,
                formData: data,
                success: (res) => {
                    tt.hideLoading()
                    var api_data = JSON.parse(res.data)
                    resolve(api_data)
                },
                fail: function (res) {
                    reject(res)
                }
            })
        })
    },

    // 正则扫码海报过来的参数
    sceneToObject(scene) {
        if (scene) {
            scene = decodeURIComponent(scene)
            let ret = {}
            let regParam = /([^&=]+)=([\w\W]*?)(&|$|#)/g
            let strParam = scene
            let result
            while ((result = regParam.exec(strParam)) != null) {
                ret[result[1]] = result[2]
            }
            return ret
        } else {
            return {}
        }
    },

    // 自动更新
    autoUpdate: function () {
        if (tt.canIUse('getUpdateManager')) {
            const updateManager = tt.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
                if (res.hasUpdate) {
                    updateManager.onUpdateReady(function () {
                        tt.showModal({
                            title: '更新提示',
                            content: '新版本已经准备好，是否重启应用？',
                            success: function (res) {
                                if (res.confirm) {
                                    // 新的版本已经下载好，调用applyUpdate应用新版本并重启
                                    updateManager.applyUpdate()
                                }
                            }
                        })
                    })
                    updateManager.onUpdateFailed(function () {
                        // 新的版本下载失败
                        tt.showModal({
                            title: '更新提示~',
                            content: '发现新版本，请删除当前小程序，重新搜索打开~'
                        })
                    })
                }
            })
        }
    },

    //得到指定增减天数的日期
    getDateStr(addDay) {
        //getDateStr(-1) 前天
        //getDateStr(0) 今天
        //getDateStr(1) 明天
        var date
        date = new Date()
        date.setDate(date.getDate() + addDay)
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        if (m < 10) {
            m = '0' + m
        }
        if (d < 10) {
            d = '0' + d
        }
        return y + "-" + m + "-" + d
    }
})